<?php

namespace app\Cron\controller;

use app\api\model\Passenger;
use app\base\service\Redis;
use think\Db;
use think\Exception;
use think\exception\DbException;

class Order
{
    /**
     * 新系统生成订单超过24小时自动支付【每6小时执行一次】
     */
    public function autoPay(){
        $where['order_state'] = 9;
        $where['is_old']      = 0;//新系统订单
        //$where['order_type']  = ['in','1,2'];
        $where['arrival_time']= ['< time',date('Y-m-d H:i:s',time()-24*60*60)];
        try{
            $orders = Db::name('order')
                ->alias('a')
                ->join('order_gps b','a.order_id = b.order_id')
                ->join('order_fee c','a.order_id = c.order_id')
                ->where($where)
                ->field('a.order_id,order_no,pay_type,passenger_id,order_type,passenger_money,a.use_type')
                ->limit(10)
                ->select();
        }catch (DbException $e){
            return;
        }
        $passenger = new Passenger();
        foreach($orders as $order){
            $payMoney = $order['use_type'] == 1 ? 0 : $order['passenger_money'];
            $ret   = $passenger->orderPay($order['order_id'],$order['passenger_id'],$payMoney,$order['pay_type']);
            if($ret[0]==1){
                 //Hook::listen('auto_pay',$order['order_id']);
            }else{
                 //Hook::listen('pay_error',$order['order_id']);
            }
        }
    }

    /**
     * 订单同步，旧系统到新系统的同步
     * 定时执行 每1分钟执行1次
     */
    public function oldToNewOrderSync()
    {
        // 状态同步
        $sql = 'update p_order a left join jjcx_com.p_order b on a.order_no = b.order_no set a.order_state = b.order_state where a.order_state<> b.order_state';
        Db::execute($sql);
        // 订单数量更新
        $sql = 'update p_driver b left join (select driver_id,count(order_id) order_count from p_order where order_state in (9,10,11) group by driver_id) a  on a.driver_id = b.driver_id  set b.order_count = a.order_count where a.driver_id is not null and b.order_count <> a.order_count';
        Db::execute($sql);
        // 费用同步
        $sql = 'update p_order_fee a left join p_order b on a.order_id =b.order_id left join jjcx_com.order_apply c on b.order_no = c.apply_number left join jjcx_com.order_apply_allot d on c.id = d.apply_id left join jjcx_com.order_allot_detail e on d.allot_id = e.allot_id set a.passenger_money=e.customer_money,a.driver_money=e.driver_money,a.company_money=e.company_money where e.customer_money is not null and a.passenger_money<>e.customer_money';
        Db::execute($sql);
        // 订单同步
        $orderIds    = Db::name('order_distinct')->limit(10)->order('order_id')->column('order_id');
        if(empty($orderIds)) return;
        $orderIdsStr = implode(",", $orderIds);
        $orderGps    = Db::name('order_gps_old')->where("order_id in ($orderIdsStr)")->select();
        $orderFee    = Db::name('order_fee_old')->where("order_id in ($orderIdsStr)")->select();

        $orders      = Db::name('order_distinct')->limit(10)->order('order_id')->select();
        if(empty($orders)) return;
        $temp        = [];
        $orderNoArr  = [];
        $typeChange  = [
            127=>142,
            126=>146,
            128=>150,
            111=>137,
            117=>143,
            114=>140,
            115=>141,
            113=>139,
            112=>138,
            118=>144,
            119=>145,
            121=>148,
            122=>149,
            120=>147,
            123=>151,
            81=>134,
            80=>133,
            79=>135,
            83=>154
        ];
        $redis = Redis::getInstance();
        foreach ($orders as $v) {
            unset($v['order_id']);
            $orderNoArr[]     = $v['order_no'];
            $v['car_type_id'] = $typeChange[$v['car_type_id']] ?: $v['car_type_id'];
            // 同步订单异常检测队列
            $redis->push('check_order_exception',$v['order_no']);
            $temp[]           = $v;
        }
        // 开启事物，插入失败则回滚
        Db::startTrans();
        try {
            Db::name('order')->insertAll($temp);
            $orderNoStr = "'" . implode("','", $orderNoArr) . "'";
            $ids        = Db::name('order')->where("order_no in ($orderNoStr)")->order('order_id')->column('order_no,order_id');
            $gps        = [];
            foreach ($orderGps as $v2) {
                $v2['order_id'] = $ids[$v2['order_no']];
                unset($v2['order_no']);
                $gps[] = $v2;
            }
            $fee = [];
            foreach ($orderFee as $v3) {
                $v3['order_id'] = $ids[$v3['order_no']];
                unset($v3['order_no']);
                $fee[] = $v3;
            }
            Db::name('order_gps')->insertAll($gps);
            Db::name('order_fee')->insertAll($fee);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }

        return 'ok';
    }
}
